<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 添加分类按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="addClassify">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 树形表格区域 -->
      <!-- data是表格绑定的数据 columns是列绑定的内容 -->
      <tree-table
        :data="catList"
        :columns="columns"
        show-index
        index-text="#"
        :expand-type="false"
        :selection-type="false"
        :show-row-hover="false"
        class="TreeTable"
      >
        <!-- 是否有效区域 -->
        <template slot="isdeleted" slot-scope="scope">
          <i v-if="scope.row.cat_deleted==false" class="el-icon-success" style="color:#20B2AA"></i>
          <i v-else class="el-icon-error" style="color:#F56C6C"></i>
        </template>
        <!-- 排序区域 -->
        <template slot="islevel" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level===0" size="mini">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level===1" size="mini" type="success">二级</el-tag>
          <el-tag v-else size="mini" type="warning">三级</el-tag>
        </template>
        <!-- 操作区域 -->
        <template slot="isopt" slot-scope="scope">
          <el-button size="mini" icon="el-icon-edit" type="primary">编辑</el-button>
          <el-button size="mini" icon="el-icon-delete" type="danger">删除</el-button>
        </template>
      </tree-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3,5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="catdialogVisible"
      width="50%"
      @close="addCtaDialogColsed"
    >
      <el-form :model="addCatFrom" :rules="addCatFromRules" ref="addCatFromRef" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCatFrom.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <!-- model绑定存储选中的父级id options绑定的是父级分类的数据 props绑定的是配置对象 -->
          <el-cascader
            v-model="ParentIdList"
            :options="ParentList"
            :props="cascaderProps"
            expand-trigger="hover"
            @change="parentCatChanged"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="catdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCat">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //   商品分类数据列表显示
      catList: [],
      //   查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 总数据条数
      total: 0,
      // 表格中每一列的数据内容
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          prop: "cat_deleted",
          type: "template",
          template: "isdeleted"
        },
        {
          label: "排序",
          type: "template",
          template: "islevel"
        },
        {
          label: "操作",
          type: "template",
          template: "isopt"
        }
      ],
      //添加分类对话框隐藏
      catdialogVisible: false,
      // 添加分类的数据
      addCatFrom: {
        // 分类名称
        cat_name: "",
        // 分类父级id
        cat_pid: [0],
        // 分类层级
        cat_level: 0
      },
      // 添加分类表单时验证规则
      addCatFromRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" }
        ]
      },
      // 父级分类的数据列表
      ParentList: [],
      // 存储选中的父级的分类的id
      ParentIdList: [],
      // 指定级联选择器的配置对象
      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        checkStrictly: true
        // multiple: true
      }
    };
  },
  methods: {
    // 获取商品分类列表数据
    async getCatList() {
      const { data } = await this.$http.get("categories", {
        params: this.queryInfo
      });
      if (data.meta.status !== 200) {
        return this.$message.error(data.meta.msg);
      }
      // console.log(data);
      this.catList = data.data.result;
      this.total = data.data.total;
    },
    // 分页pagesizi发生变化
    handleSizeChange(newSize) {
      this.pagesize = newSize;
    },
    // 分页pagenum发生变化
    handleCurrentChange(newNum) {
      this.pagenum = newNum;
    },
    // 点击添加分类按钮
    async addClassify() {
      this.getParentList();
      this.catdialogVisible = true;
    },
    // 获取父级列表的数据
    async getParentList() {
      const { data } = await this.$http.get("categories", {
        params: { type: 2 }
      });
      // console.log(data);
      if (data.meta.status !== 200) {
        return this.$message.error(data.meta.msg);
      }
      this.ParentList = data.data;
    },
    // 获取到选中的父级节点的id值
    parentCatChanged() {
      // 如果打印的数据长度大于0 就说明选中了值 需要重新赋值
      if (this.ParentIdList.length > 0) {
        this.addCatFrom.cat_pid = this.ParentIdList[
          this.ParentIdList.length - 1
        ];
        this.addCatFrom.cat_level = this.ParentIdList.length;
        console.log(this.ParentIdList);
      } else {
        this.addCatFrom.cat_pid = 0;
        this.addCatFrom.cat_level = 0;
      }
    },
    // 点击添加按钮 添加新的分类
    addCat() {
      console.log(this.addCatFrom);
      // 表单的预验证
      this.$refs.addCatFromRef.validate(async valid=>{
        if(!valid){
          return this.message.error('验证失败')
        }
        const {data}=await this.$http.post('categories',this.addCatFrom)
        console.log(data);
        if(data.meta.status!==201){
          return this.$message.error(data.meta.msg)
        }
        this.getCatList()
        this.$message.success(data.meta.msg)
        this.catdialogVisible=false
        
      })
    },
    // 关闭对话框的事件
    addCtaDialogColsed() {
      this.$refs.addCatFromRef.resetFields();
      this.ParentIdList = [];
      this.addCatFrom.cat_pid = 0;
      this.addCatFrom.cat_level = 0;
    }
  },
  created() {
    this.getCatList();
  }
};
</script>

<style lang="less" scoped>
.TreeTable {
  margin-top: 20px;
}
.el-cascader {
  width: 100%;
}
</style>